package com.wh.debeziumdemo.strategy.impl;

import com.alibaba.fastjson.JSON;
import com.wh.debeziumdemo.listener.MysqlBinlogListener;
import com.wh.debeziumdemo.po.Student;
import com.wh.debeziumdemo.strategy.StudentStrategy;
import io.debezium.data.Envelope;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @description: 学生新增时
 * @author: GooGoo
 * @time: 2023/8/19 23:20
 */
@Service
@Slf4j
public class StudentInsertMysqlBinlogService extends StudentStrategy {

  @Autowired
  RedisTemplate redisTemplate;

  @Override
  public String getOperation() {
    return Envelope.Operation.CREATE.code();
  }

  @Override
  public void doOperation(MysqlBinlogListener.ChangeData changeData) {
    log.info("student insert: {}", changeData.getAfter());
    //学生表插入时执行，在这里可以存redis、es等，这里只演示
    Student student = JSON.parseObject(JSON.toJSONString(changeData.getAfter()), Student.class);
    redisTemplate.opsForValue().set(this.getRedisPrefix()+student.getId(),JSON.parse(student.toString()));
  }
}
